package org.workplicity.cia.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.ExecutionException;

import org.workplicity.cia.ui.android.MainActivity;
import org.workplicity.elog.entry.ElogUser;
import org.workplicity.elog.entry.ElogUser.Function;
import org.workplicity.task.NetTask;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import com.mongodb.BasicDBObject;
import com.mongodb.util.JSON;

import android.os.AsyncTask;
import android.widget.Toast;

public class AndroidHelper extends Helper {

	static Boolean loginStatus = false;
	
	static ASyncConnect async;
	static ASyncQuery asyncQ;
	

	public AndroidHelper() {
		
	}
	
	public static boolean login(String username, String password, WorkletContext context) {
		async = new ASyncConnect();
		
		android.util.Log.d("alarm", "Start Login");
        NetTask.setUrlBase("http://192.168.0.10:8080/netprevayle/task");
        NetTask.setStoreName("nl");
        
		try {
			async.execute(username, password, context).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return true;

		
	}
	
	public static ArrayList query(String arg0, String arg1, WorkletContext context) {
		asyncQ = new ASyncQuery();
		
		
			asyncQ.execute(arg0, arg1, context);
		
		
		return null;
		
	}
	
	
	
	
	
	
	
	
	
	public static Boolean getLoginStatus() {
		return loginStatus;
	}

	public static void setLoginStatus(Boolean loginStatus) {
		AndroidHelper.loginStatus = loginStatus;
	}
	

	
}


class ASyncConnect extends AsyncTask<Object, Integer, Integer> {

	@Override
	protected Integer doInBackground(Object... arg0) {
		// create success member
        Integer success = 0;
        
		try {
	        
	        // cast the variables comming in
	        String username = arg0[0].toString();
	        String password = arg0[1].toString();
	        WorkletContext context = (WorkletContext)arg0[2];
	        
	        if (!Helper.login(username, password, context)) {
	        	android.util.Log.d("alarm", "Failed Login");
	        	
	        	
	        	return success;
	        }
	        else {
	        	android.util.Log.d("alarm", "************Login Success****************");
	        	success = 1;
	        	return success;
	        }
	       
        }
        catch(Exception e) {
        	return success;
        }
	}
	
	protected void onPostExecute(Integer success) {
        if(success > 0) {
        	AndroidHelper.setLoginStatus(true);
        }
        else {
        	android.util.Log.d("alarm", "%%%%%%%%%HELP%%%%%%%%%");
        	
        }
        
        android.util.Log.d("alarm", "!All done2222222!");
    }
	
	
}


class ASyncQuery extends AsyncTask<Object, Integer, ArrayList<ElogUser>> {

	@Override
	protected ArrayList<ElogUser> doInBackground(Object... arg0) {
		try {
	        
	        // cast the variables comming in
			String repos = arg0[0].toString();
	        String criteria = arg0[1].toString();
	        WorkletContext context = (WorkletContext)arg0[2];
	        
	        ArrayList<ElogUser> users = Helper.query(repos, criteria, context);
	        	
	       
	        return users;
	       
        }
        catch(Exception e) {
        	return null;
        }
	}
	
	protected void onPostExecute(ArrayList<ElogUser> users) {
        for(ElogUser user : users) {
        	android.util.Log.d("alarm", "!!!!!!!!!!!User: " + user.getFirstName() + " " + user.getLastName());
        }
        
        android.util.Log.d("alarm", "!All done!");
        
        // refresh
        //MainActivity.this.updateUsers(users);
        
    }

}